//
//  SXTopicMainRightViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXTopicMainRightViewController.h"
#import "SXTopicMainRightTableViewCell.h"
#import "SXTpoicMainModel.h"
#import "UIImageView+WebCache.h"
#import "SXTopicListViewController.h"

@interface SXTopicMainRightViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) UIAlertController *alertTs;

@end

@implementation SXTopicMainRightViewController

- (void)setSubscript:(NSInteger)subscript
{
    _subscript = subscript;
    [self setWithData];
}

- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        self.dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (NSMutableArray *)listArray
{
    if (!_listArray) {
        self.listArray = [NSMutableArray array];
    }
    return _listArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth * 4/5, kScreenHeight - 113) style:(UITableViewStylePlain)];
    _rightTableView.dataSource = self;
    _rightTableView.delegate = self;
    _rightTableView.rowHeight = 50;
    _rightTableView.tableFooterView =  [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth * 4/5, 0.5)];
    _rightTableView.tableFooterView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:_rightTableView];
    
    [_rightTableView registerClass:[SXTopicMainRightTableViewCell class] forCellReuseIdentifier:@"rightTableView"];
    
    _subscript = 0;
    
    [self requsetListArray];
    [self setWithData];
}
// 解析
- (void)requsetListArray
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HomeModel" ofType:@"plist"];
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePath];
    
    for (NSArray *listArr in dataArray) {
        [self.listArray addObject:listArr];
    }
}

// 解析数据
- (void)setWithData
{
    self.dataSourceArray = nil;
    for (NSDictionary *modelDic in _listArray[_subscript]) {
        SXTpoicMainModel *topicMainModel = [[SXTpoicMainModel alloc] init];
        [topicMainModel setValuesForKeysWithDictionary:modelDic];
        [self.dataSourceArray addObject:topicMainModel];
    }
    
    [_rightTableView reloadData];
}

// 点击 cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXTopicListViewController *listVC = [[SXTopicListViewController alloc] init];
    listVC.forum_id = [_dataSourceArray[indexPath.row] forum_id];
    listVC.titleItem = _titleItem;
    [_rootVC.navigationController pushViewController:listVC animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listArray[_subscript] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXTopicMainRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightTableView" forIndexPath:indexPath];
    SXTpoicMainModel *model = _dataSourceArray[indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.avatarImage sd_setImageWithURL:[NSURL URLWithString:model.avatar_url]];
    cell.titleLabel.text = model.forum_name;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
